package 笔试;

import java.util.Scanner;

/**
 * @Date 2024/10/12 10:37
 * @description: 京东笔试: 路径问题, 求最小步数
 * .
 * @Author LittleNight
 */
public class JD3 {

    public static void main1(String[] args) {
        String s = "abcd";
        System.out.println(s.substring(0, 2));
    }

    // 测试通过
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = in.nextInt(), m = in.nextInt();
            char[][] grid = new char[n][m];
            for (int i = 0; i < n; i++) {
                String tmp =  in.next();
                char[] ch = tmp.toCharArray();
                for (int j = 0; j < m; j++) {
                    grid[i][j] = ch[j];
                }
            }
            if(grid[n-2][m-1] == '#' && grid[n-1][m-2] == '#') {
                System.out.println("-1");
            }
            int[][] dp = new int[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    dp[i][j] = Integer.MAX_VALUE;
                    // 走到这的最小步数
                    if(grid[i][j] == '.') {
                        int ret1 = 0, ret2 = 0;
                        for (int k = 0; k < i; k++) { // 列
                            if(dp[k][j] == '.') ret1 = Math.min(ret1, dp[k][j]);
                        }
                        for (int k = 0; k < j; k++) {
                            if(dp[i][k] == '.') ret2 = Math.min(ret2, dp[i][k]);
                        }
                        dp[i][j] = Math.min(ret1, ret2) + 1;
                    }
                }
            }
            System.out.println(dp[n - 1][m - 1]);
        }
    }

    private static String langFront(String s1, String t1) {
        char[] s = s1.toCharArray(), t = t1.toCharArray();
        int m = s.length, n = t.length;
        StringBuffer sb = new StringBuffer();
        int i = 0, j = 0;
        while(i < m && j < n) {
            if(s[i] == t[j]) {
                i++;
                j++;
            } else {
                break;
            }
        }
        // 有一种情况, 出来了
        if(i == m) return s1;
        if(j == n) return t1;
        return s1.substring(0, i + 1); // (]
    }
}
